<%@page import="monster.comm.util.SpringApplicationContextFactory"%>
<%@page import="monster.flight.admin.manager.service.admin.Flight_infoService"%>
<%@page import="monster.flight.comm.util.FlightFactory"%>
<%@page import="monster.comm.util.DateUtil"%>
<%@page import="monster.flight.comm.entity.vo.FlightBay"%>
<%@page import="sun.jdbc.rowset.CachedRowSet"%>
<%@page import="monster.flight.comm.entity.vo.Flight_info"%>
<%@page import="monster.comm.database.DBManager"%>
<%@page import="monster.comm.util.SysConstant"%>
<%@page import="monster.comm.util.UserService"%>
<%@page import="monster.comm.util.Tool"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String type=Tool.getRequest("type",request);
String flight_id=Tool.getRequest("flight_id",request);
String flight_no=Tool.getRequest("flight_no",request);
String ac_type=Tool.getRequest("ac_type",request);
String ac_reg=Tool.getRequest("ac_reg",request);
String now_time=Tool.getRequest("now_time",request);
String bay=Tool.getRequest("bay",request);
String ret="";
if(flight_id.equals(""))flight_id="0";
String s_staff_name = session.getAttribute("STAFF_NAME");
String s_staff_sn = session.getAttribute("STAFF_SN");

if(s_staff_name==null){
	out.println("{\"id\":-100,\"name\":\"没有登录\"}");
	return;
}else{
	try{
		DBManager db = new DBManager();
		String dateTime = DateUtil.getDate("yyyy-MM-dd HH:mm:ss");
		String date = now_time;	
		
		List<String> list = new ArrayList<String>();
		String sql="select staff_sn from plan_info where flight_id="+flight_id;
		CachedRowSet rs = db.getRowSet(sql);
		Set<String> oldSet = new HashSet<String>();//此航班已有工作安排
		Map<String,String> newMap = new HashMap<String,String>();
		Set<String> numSet = new HashSet<String>();//当天工作任务数
		while(rs.next()){
			String sn = Tool.getRsValue(rs,"staff_sn");
			oldSet.add(sn);
			
		}
		//先删除	
		for(String sn : oldSet){
			//删除工作安排
			sql="delete from PLAN_INFO where flight_id="+flight_id+" and staff_sn="+sn;
			list.add(sql);
			//此人之前被新增一定有每天工作任务数不用判断直接更新
			sql="update STAFF_DAY_WORK set TOTAL_WORKNUM=TOTAL_WORKNUM-1,CUR_DAY_WORKNUM=CUR_DAY_WORKNUM-1 where work_date='"+date+"' and staff_sn="+sn;
			list.add(sql);
		}
		//新增
		sql="select OP_DATETIME from plan_info where bay='"+bay+"' order by OP_DATETIME desc limit 1";
		rs = db.getRowSet(sql);
		String op_datetime = "";
		
		if(rs.next()){
			op_datetime = Tool.getRsValue(rs,"op_datetime");			
		}
		//查询到达人员
		sql="select STAFF_NAME,STAFF_SN from plan_info  where op_datetime='"+op_datetime+"' and bay='"+bay+"'";
		rs = db.getRowSet(sql);
		while(rs.next()){
			String sn = Tool.getRsValue(rs,"STAFF_SN");
			String name = Tool.getRsValue(rs,"STAFF_NAME");
			newMap.put(sn,name);
		}
		sql="select staff_sn from STAFF_DAY_WORK where work_date='"+date+"'";//查询当天工作任务数
		rs = db.getRowSet(sql);
		while(rs.next()){
			String sn = Tool.getRsValue(rs,"staff_sn");
			numSet.add(sn);
			
		}
		for(String sn : newMap.keySet()){
			//新增此航班的工作安排
			sql="insert into PLAN_INFO(PLAN_DATE,STAFF_NAME,STAFF_SN,FLIGHT_ID,FLIGHT_NO,AC_TYPE,AC_REG,BAY,op_name,op_sn,op_datetime)"
				+"values('"+date+"','"+newMap.get(sn)+"','"+sn+"','"+flight_id+"','"+flight_no+"','"+ac_type+"','"+ac_reg+"','"+bay+"','"+s_staff_name+"',"+s_staff_sn+",'"+dateTime+"')";
			list.add(sql);
			//判断每天工作记录
			if(numSet.contains(sn)){//有更新任务数
				sql="update STAFF_DAY_WORK set TOTAL_WORKNUM=TOTAL_WORKNUM+1,CUR_DAY_WORKNUM=CUR_DAY_WORKNUM+1 where work_date='"+date+"' and staff_sn="+sn;
			}else{//无新增
				sql="insert into STAFF_DAY_WORK(WORK_DATE,STAFF_NAME,STAFF_SN,TOTAL_WORKNUM,CUR_DAY_WORKNUM,op_datetime)"
					+"values('"+date+"','"+newMap.get(sn)+"','"+sn+"',1,1,'"+dateTime+"')";
			}					
			list.add(sql);
		}
		if(list.size()>0){
			String [] sqls = new String[list.size()];
			list.toArray(sqls);
			db.batchMoreUpdate(sqls);
		}
	
		ret="{\"id\":100}";

		
	}catch(Exception e){
		 e.printStackTrace();
		 ret="{\"id\":-200,\"name\":\"程序异常\"}";
	}
	
	out.println(ret);

}
%>
